import axios from 'axios'


axios.defaults.baseURL = '//klxin.cn:8888/api/private/v1'

import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式

axios.interceptors.request.use(


  config => {

    NProgress.start() // 进度条开启
    const token = window.sessionStorage.getItem('token')
    if (token) { // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers.Authorization = token //请求头加上token
    }
    return config
  },
  err => {
    return Promise.reject(err)
  })

axios.interceptors.response.use(config => {
  //当进入response拦截器，表示请求已经结束，我们就结束进度条
  NProgress.done() // 进度条关闭
  return config
})


export const login_api = (obj) => {
  return axios.post('/login', obj)
}


export const menus_api = () => {
  return axios('/menus')
}

export const getusers_api = (obj) => {
  return axios('/users', obj)
}

export const chandestatus_api = obj => {
  return axios.put(`/users/${obj.uId}/state/${obj.type}`)

}

export const deluser_api = id => {
  return axios.delete(`/users/${id}`)

}

export const edituser_api = obj => {
  return axios.put(`/users/${obj.id}`, {
    email: obj.email,
    mobile: obj.mobile
  })

}


export const adduser_api = obj => {
  return axios.post(`/users`, {
    username: obj.username,
    password: obj.password,
    email: obj.email,
    mobile: obj.mobile
  })

}

export const rightlist_api = () => {
  return axios(`/rights/list `)

}

export const getroles_api = () => {
  return axios(`/roles`)

}

export const rolesdel = (obj) => {
  return axios.delete(`/roles/${obj.roleId}/rights/${obj.rightId}`)

}

export const rolesrights = () => {
  return axios(`rights/tree`)

}



export const addrolesrights = (obj) => {
  return axios.post(`/roles/${obj.roleId}/rights`, {
    rids: obj.rids
  })

}


export const goodslist_api = (obj) => {
  return axios(`/goods`, {
    params: {
      query: obj.query,
      pagenum: obj.pagenum,
      pagesize: obj.pagesize
    }
  })
}


export const goodscategories_api = (obj) => {
  return axios(`/categories`, {
    params: {
      type: 3,
      pagenum: obj.pagenum,
      pagesize: obj.pagesize
    }
  })
}

export const dynamic_api = (obj) => {
  return axios(`categories/${obj.id}/attributes`, {
    params: {
      sel: obj.sel
    }
  })
}

export const static_api = (obj) => {
  return axios(`categories/${obj.id}/attributes`, {
    params: {
      sel: 'only'
    }
  })
}


export const addcategories_api = (obj) => {
  return axios.put(`categories/${obj.id}/attributes/${obj.attrId}`, {

    attr_name: obj.attr_name,
    attr_sel: obj.attr_sel,
    attr_vals: obj.attr_vals

  })
}

export const editcategories_api = (obj) => {
  return axios.put(`categories/${obj.id}/attributes/${obj.attrId}`, {
    attr_name: obj.attr_name,
    attr_sel: obj.attr_sel,
  })
}

export const goodsAdd_api = (obj) => {
  return axios.post(`goods`, obj)
}

export const reports_api = (obj) => {
  return axios(`reports/type/1`)
}  
